<template>
  <div>
    <div style="width: 90%; display: flex; justify-content: space-between">
      <span>新增充电站</span>
      <el-button type="primary" @click="submitForm">保存</el-button>
    </div>
    <el-form
      ref="elForm"
      :model="formData"
      :rules="rules"
      size="medium"
      label-width="100px"
    >
      <el-row>
        <el-col :span="10">
          <el-form-item label="充电站名称" prop="stationName">
            <el-input
              v-model="formData.stationName"
              placeholder="请输入充电站名称"
              :maxlength="11"
              show-word-limit
              clearable
              prefix-icon="el-icon-mobile"
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="充电站编号" prop="stationCode">
            <el-input
              v-model="formData.stationCode"
              placeholder="请输入充电站编号"
              clearable
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="充电站类型" prop="stationType">
            <el-input
              v-model="formData.stationType"
              placeholder="请输入充电站类型"
              clearable
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="电站位置" prop="stationLocation">
            <el-input
              v-model="formData.stationLocation"
              placeholder="请输入电站位置"
              clearable
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item
            label-width="110px"
            label="最大充电桩数"
            prop="maxChargerNum"
          >
            <el-input
              v-model="formData.maxChargerNum"
              placeholder="最大充电桩数"
              clearable
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="电站负责人" prop="stationManager">
            <el-select
              v-model="formData.stationManager"
              placeholder="请选择电站负责人"
              clearable
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="(item, index) in stationManagerOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="收款金额" prop="chargeMoney">
            <el-select
              v-model="formData.chargeMoney"
              placeholder="请选择收款金额"
              clearable
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="option in chargeMoneyOptions"
                :key="option.value"
                :label="option.label"
                :value="option.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="图片" prop="image" required>
        <el-upload
          ref="image"
          :file-list="imagefileList"
          :action="imageAction"
          :on-success="handleUploadSuccess"
          :before-upload="imageBeforeUpload"
          list-type="picture-card"
        >
          <i class="el-icon-plus"></i>
          <div slot="tip" class="el-upload__tip">只能上传不超过 2MB 的文件</div>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import axios from "axios";
export default {
  components: {},
  props: [],
  data() {
    return {
          // 动态选项数组
          chargeMoneyOptions: [
        { label: '收费方案1', value: '1' },
        { label: '收费方案2', value: '2' },
        { label: '收费方案3', value: '3' },
      
      ],
      formData: {
        stationName: undefined,
        stationCode: undefined,
        stationType: "",
        stationLocation: undefined,
        maxChargerNum: undefined,
        stationManager: undefined,
        image: null,
      },
      rules: {
        stationName: [
          {
            required: true,
            message: "请输入充电站名称",
            trigger: "blur",
          },
        ],
        stationCode: [
          {
            required: true,
            message: "请输入充电站编号",
            trigger: "blur",
          },
        ],
        stationType: [
          {
            required: true,
            message: "请输入充电站类型",
            trigger: "blur",
          },
        ],
        stationLocation: [
          {
            required: true,
            message: "请输入电站位置",
            trigger: "blur",
          },
        ],
        maxChargerNum: [
          {
            required: true,
            message: "最大充电桩数",
            trigger: "blur",
          },
        ],
        stationManager: [
          {
            required: true,
            message: "请选择电站负责人",
            trigger: "change",
          },
        ],
      },
      imageAction: "/bdc/bdc/upload",
      imagefileList: [],
      stationManagerOptions: [
        {
          label: "选项一",
          value: 1,
        },
        {
          label: "选项二",
          value: 2,
        },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    handleUploadSuccess(response, file, fileList) {
      // 处理上传成功后的逻辑
      console.log("File uploaded successfully:", response);
      this.formData.image = response; // 将后端返回的文件路径保存到表单字段中
    },
    submitForm() {
      console.log(this.formData);
      axios
        .post("/bdc/bdc/addCharging", this.formData)
        .then((res) => {
          console.log(res);
          this.$message.success("保存成功");
          this.$router.push({
            path: "/chargingStationList",
          });
        })
        .catch((error) => {
          console.log(error);
        });
    },
    resetForm() {
      this.$refs["elForm"].resetFields();
    },
    imageBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2;
      if (!isRightSize) {
        this.$message.error("文件大小超过 2MB");
      }
      return isRightSize;
    },
    handleChange(value) {
      console.log("Selected:", value); // 打印选中的值
    },
  },
};
</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}
</style>
